import CryptoJS from 'crypto-js'

export default {
  // 微信登录
  wechatlogin (url) {
    let href = url || window.location.href
    // 增加quserId
    // eslint-disable-next-line
    // href = href.replace(/&?token=[a-zA-Z0-9\.\-\_ ]*/g, '').replace(/&?quserId=\d*/g, '').replace('userId', 'quserId')
    window.location.href = process.env.BASE_URL + 'smartShoes/web/wechatLogin.json?url=' + window.encodeURIComponent(href)
  },
  // DES加密
  encrypt (value, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key)
    var encrypted = CryptoJS.DES.encrypt(value, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
    return {
      key: keyHex,
      value: encrypted.toString()
    }
  },
  // DES解密
  decrypt (value, key) {
    var plaintext = CryptoJS.DES.decrypt(value, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
    return plaintext.toString(CryptoJS.enc.Utf8)
  },
  // DES参数处理
  getQueryString (name, string) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = string.match(reg)
    if (r != null) return unescape(r[2])
    return null
  },
  // 下载app
  download () {
    let u = navigator.userAgent
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1 // g
    let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
    let url = ''
    if (isAndroid) {
      url = 'http://sj.qq.com/myapp/detail.htm?apkName=com.odun.odunRunning'
    } else if (isIOS) {
      url = 'https://itunes.apple.com/cn/app/%E6%AC%A7%E7%9B%BE%E9%85%B7%E8%B7%91/id1279128697?mt=8'
    } else {
      url = 'http://sj.qq.com/myapp/detail.htm?apkName=com.odun.odunRunning'
    }
    window.location.href = url
  }
}
